package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaeT;
import cn.e4j.modular.crm.entity.GwbsT;
import cn.e4j.modular.crm.entity.GwdbT;
import cn.e4j.modular.crm.entity.GwfcT;
import cn.e4j.modular.crm.service.GwaeTService;
import cn.e4j.modular.crm.service.impl.GwfcTServiceImpl;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 期费用预提管理
 * Created by shixc on 2019-10-25.
 */
@Controller
@RequestMapping("/crm/gwft200")
public class Gwft200Controller extends BaseController {

    @Autowired
    private GwaeTService gwaeTService;

    @Autowired
    private GwfcTServiceImpl gwfcTService;

    /**
     * 跳转期费用预提管理页面
     */
    @RequiresPermissions("crm:gwft200:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwft200.html";
    }

    /**
     * 查询期费用预提信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ENT",shiroUser.getDBC());
        inDto.put("lang",LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwfcTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取新增申请单号
     */
    @RequestMapping("/getGwfcdocno")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData getGwdb001(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = Dtos.newDto();
        Date date = new Date();
        addDataDto.put("GWFCDOCNO","FYYT" + DateUtils.formatDate(date, "yyyyMMddHHmmssSSS"));
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 获取汇率
     */
    @ResponseBody
    @RequiresPermissions("crm:gwft200:init")
    @RequestMapping("/getRate")
    public ResponseData getRate(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = gwfcTService.getRate(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfcT> gwfcTList = inDto.getReqList(GwfcT.class);
        ShiroUser shiroUser = ShiroKit.getUser();
        gwfcTService.addData(gwfcTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询期费用预提信息
     */
    @RequestMapping("/listGwfc")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData listGwfc(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwfcTService.listGwfc(inDto);
        return ResponseData.success(result);
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfcT> gwfcTList = inDto.getReqList(GwfcT.class);
        gwfcTService.editData(gwfcTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwft200:del")
    @ResponseBody
    public ResponseData del(GwfcT gwfdT) {
        gwfcTService.delete(gwfdT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwft200:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfcT> gwfcTList = inDto.getReqList(GwfcT.class);
        gwfcTService.delDataBatch(gwfcTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwdc002:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfcT> gwfcTList = inDto.getReqList(GwfcT.class);
        gwfcTService.updateStatus(gwfcTList);
        return SUCCESS_TIP;
    }

    /**
     * 批次产生前的判断
     */
    @RequestMapping("/beforeGeneration")
    @RequiresPermissions("crm:gwat550:init")
    @ResponseBody
    public ResponseData beforeGeneration(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto countDto = gwfcTService.getCount(inDto);
        return ResponseData.success(countDto);
    }

    /**
     * 批次产生
     */
    @RequestMapping("/batchGeneration")
    @RequiresPermissions("crm:gwft200:init")
    @ResponseBody
    public ResponseData autoGeneration(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        try {
            outDto = gwfcTService.autoGeneration(inDto);
        }catch (Exception e){
            e.printStackTrace();
            outDto.put("message", "批次生成失败！");
            return ResponseData.success(outDto);
        }
        return ResponseData.success(outDto);
    }

}
